package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.DsAdviseRecordDao;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.entity.DsAdviseRecordEntity;
import com.ym.medical.service.IDoctorStudioSV;
import com.ym.medical.service.IDsAdviseRecordSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Redis保存全部待抢订单：
1、推荐给全科医生抢单时，全科医生刷新页面获取订单，待全科医生抢单成功，插入工Service业务层处理
*
* @author ym
* @date 2020-09-14
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class DsAdviseRecordSVImpl extends BaseSVImpl<DsAdviseRecordEntity,DsAdviseRecordDao> implements IDsAdviseRecordSV {

	@Autowired
	private IDoctorStudioSV doctorStudioSV;

	@Override
	public List<Long> saveDoctorAdvise(Long orderId, Long[] doctorId, Long currId, Long currDoctorStudioId) {
		List<Long> saveIds = new ArrayList<>();
		for (Long id : doctorId) {
			DsAdviseRecordEntity dsAdviseRecord = new DsAdviseRecordEntity();
			dsAdviseRecord.setAdviseDoctorId(currId);
			dsAdviseRecord.setBeadvisedDoctorId(id);
			dsAdviseRecord.setCreateTime(new Date());
			dsAdviseRecord.setAdviseDsId(currDoctorStudioId);
			// 查询被推荐医生的工作室ID
			DoctorStudioEntity doctorStudio = doctorStudioSV.selectOne(DoctorStudioEntity.builder().belongPerson(id).build());
			if (doctorStudio != null) {
				dsAdviseRecord.setBeadvisedDsId(doctorStudio.getId());
			}
			dsAdviseRecord.setOrderId(orderId);
			this.insert(dsAdviseRecord);
			saveIds.add(dsAdviseRecord.getId());
		}
		return saveIds;
	}
}
